﻿namespace MDXStudio.MdSchema
{
    using Microsoft.AnalysisServices.AdomdClient;
    using System;
    using System.Runtime.InteropServices;

    public class MdMember
    {
        private const string cCol_CATALOG_NAME = "CATALOG_NAME";
        private const string cCol_CHILDREN_CARDINALITY = "CHILDREN_CARDINALITY";
        private const string cCol_CUBE_NAME = "CUBE_NAME";
        private const string cCol_DIMENSION_UNIQUE_NAME = "DIMENSION_UNIQUE_NAME";
        private const string cCol_HIERARCHY_UNIQUE_NAME = "HIERARCHY_UNIQUE_NAME";
        private const string cCol_KEY0 = "KEY0";
        private const string cCol_LEVEL_NUMBER = "LEVEL_NUMBER";
        private const string cCol_LEVEL_UNIQUE_NAME = "LEVEL_UNIQUE_NAME";
        private const string cCol_MEMBER_KEY = "MEMBER_KEY";
        private const string cCol_MEMBER_NAME = "MEMBER_NAME";
        private const string cCol_MEMBER_UNIQUE_NAME = "MEMBER_UNIQUE_NAME";
        private const string cCol_PARENT_LEVEL = "PARENT_LEVEL";
        private const string cCol_PARENT_UNIQUE_NAME = "PARENT_UNIQUE_NAME";
        private string mCubeName;
        private string mDimensionUniqueName;
        private bool mHasChildren;
        private string mHierarchyUniqueName;
        private string mKey;
        private int mLevelNumber;
        private string mLevelUniqueName;
        private string mName;
        private int mParentLevelNumber;
        private string mParentUniqueName;
        private string mUniqueName;

        public MdMember(string pCubeName, string pUniqueName)
        {
            this.mCubeName = string.Empty;
            this.mDimensionUniqueName = string.Empty;
            this.mHierarchyUniqueName = string.Empty;
            this.mLevelUniqueName = string.Empty;
            this.mUniqueName = string.Empty;
            this.mName = string.Empty;
            this.mParentUniqueName = string.Empty;
            this.mKey = string.Empty;
            this.mCubeName = pCubeName;
            this.mUniqueName = pUniqueName;
        }

        public MdMember(string pCubeName, string pDimensionUniqueName, string pHierarchyUniqueName, string pLevelUniqueName, int pLevelNumber, string pUniqueName, string pName, string pParentUniqueName, int pParentLevelNumber, string pKey, bool pHasChildren)
        {
            this.mCubeName = string.Empty;
            this.mDimensionUniqueName = string.Empty;
            this.mHierarchyUniqueName = string.Empty;
            this.mLevelUniqueName = string.Empty;
            this.mUniqueName = string.Empty;
            this.mName = string.Empty;
            this.mParentUniqueName = string.Empty;
            this.mKey = string.Empty;
            this.mCubeName = pCubeName;
            this.mDimensionUniqueName = pDimensionUniqueName;
            this.mHierarchyUniqueName = pHierarchyUniqueName;
            this.mLevelUniqueName = pLevelUniqueName;
            this.mLevelNumber = pLevelNumber;
            this.mUniqueName = pUniqueName;
            this.mName = pName;
            this.mParentUniqueName = pParentUniqueName;
            this.mParentLevelNumber = pParentLevelNumber;
            this.mKey = pKey;
            this.mHasChildren = pHasChildren;
        }

        public static MdMember CreateFromMember(Member pMember)
        {
            string str3;
            string str4;
            string str5;
            string str7;
            int num2;
            string str8;
            string uniqueName = pMember.UniqueName;
            string caption = pMember.Caption;
            string levelName = pMember.LevelName;
            int levelDepth = pMember.LevelDepth;
            bool pHasChildren = pMember.ChildCount > 0L;
            ReadManadatoryProperties(pMember, out str3, out str4, out str5, out str8, out str7, out num2);
            return new MdMember(str3, str4, str5, levelName, levelDepth, uniqueName, caption, str7, num2, str8, pHasChildren);
        }

        private static void ReadManadatoryProperties(Member pMember, out string oCubeName, out string oDName, out string oHName, out string oKey, out string oParUName, out int oParLNumber)
        {
            MemberPropertyCollection memberProperties = pMember.MemberProperties;
            oCubeName = string.Empty;
            oDName = string.Empty;
            oHName = string.Empty;
            oKey = string.Empty;
            oParUName = string.Empty;
            oParLNumber = 0;
            for (int i = 0; i < memberProperties.Count; i++)
            {
                object obj2;
                MemberProperty property = memberProperties[i];
                string name = property.Name;
                if (name.IndexOf("CUBE_NAME") >= 0)
                {
                    obj2 = property.Value;
                    if ((obj2 != null) && !(obj2 is DBNull))
                    {
                        oCubeName = (string) obj2;
                    }
                }
                else if (name.IndexOf("DIMENSION_UNIQUE_NAME") >= 0)
                {
                    obj2 = property.Value;
                    if ((obj2 != null) && !(obj2 is DBNull))
                    {
                        oDName = (string) obj2;
                    }
                }
                else if (name.IndexOf("HIERARCHY_UNIQUE_NAME") >= 0)
                {
                    obj2 = property.Value;
                    if ((obj2 != null) && !(obj2 is DBNull))
                    {
                        oHName = (string) obj2;
                    }
                }
                else if (name.IndexOf("MEMBER_KEY") >= 0)
                {
                    obj2 = property.Value;
                    if ((obj2 != null) && !(obj2 is DBNull))
                    {
                        oKey = (string) obj2;
                    }
                }
                else if (name.IndexOf("PARENT_UNIQUE_NAME") >= 0)
                {
                    obj2 = property.Value;
                    if ((obj2 != null) && !(obj2 is DBNull))
                    {
                        oParUName = (string) obj2;
                    }
                }
                else if (name.IndexOf("PARENT_LEVEL") >= 0)
                {
                    obj2 = property.Value;
                    if ((obj2 != null) && !(obj2 is DBNull))
                    {
                        if (obj2 is string)
                        {
                            oParLNumber = int.Parse((string) obj2);
                        }
                        else
                        {
                            oParLNumber = (int) ((uint) obj2);
                        }
                    }
                }
            }
        }

        public override string ToString()
        {
            return this.Name;
        }

        public string CubeName
        {
            get
            {
                return this.mCubeName;
            }
            set
            {
                this.mCubeName = value;
            }
        }

        public string DimensionUniqueName
        {
            get
            {
                return this.mDimensionUniqueName;
            }
        }

        public bool HasChildren
        {
            get
            {
                return this.mHasChildren;
            }
        }

        public string HierarchyUniqueName
        {
            get
            {
                return this.mHierarchyUniqueName;
            }
        }

        public string Key
        {
            get
            {
                return this.mKey;
            }
        }

        public string LevelName
        {
            get
            {
                return this.mLevelUniqueName;
            }
        }

        public int LevelNumber
        {
            get
            {
                return this.mLevelNumber;
            }
        }

        public string Name
        {
            get
            {
                return this.mName;
            }
        }

        public int ParentLevelNumber
        {
            get
            {
                return this.mParentLevelNumber;
            }
        }

        public string ParentUniqueName
        {
            get
            {
                return this.mParentUniqueName;
            }
        }

        public string UniqueName
        {
            get
            {
                return this.mUniqueName;
            }
        }
    }
}

